package com.mbc.service.impl;


import com.mbc.dto.MbcAnimalShelterDTO;
import com.mbc.dto.MbcRealNameAuthenticationDTO;
import com.mbc.dto.GetMbcAnimalShelterDTO;
import com.mbc.mapper.AnimalShelterMapper;
import com.mbc.mapper.RealNameAuthenticationMapper;
import com.mbc.mapper.SignMapper;
import com.mbc.service.AnimalShelterService;
import com.mbc.vo.GetMbcAnimalShelterVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.UUID;

/**
 * <p>
 * 流浪动物庇护所表 服务实现类
 * </p>
 *
 * @author suimeng
 * @since 2024-04-17
 */
@Service
public class AnimalShelterServiceImpl implements AnimalShelterService {

    @Autowired
    private AnimalShelterMapper animalShelterMapper;

    @Autowired
    private SignMapper signMapper;

    @Autowired
    private RealNameAuthenticationMapper realNameAuthenticationMapper;

    /**
     * 新增动物庇护所
     *
     * @param userId              用户 ID
     * @param mbcAnimalShelterDTO MBC 动物收容所 DTO
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertAnimalShelter(Integer userId, MbcAnimalShelterDTO mbcAnimalShelterDTO) {
        //用UUID作为坐标点id
        String signId = UUID.randomUUID().toString();
        Integer count = signMapper.insertAnimalShelterSign(signId, mbcAnimalShelterDTO);
        LocalDateTime now = LocalDateTime.now();
        Integer maxShelterId = animalShelterMapper.getByMaxShelterId();
        //判断流浪动物庇护所表是不是为空
        if (maxShelterId == null) {
            //新增动物庇护所和庇护所坐标
            maxShelterId = 100000;
            return animalShelterMapper.insertAnimalShelter(maxShelterId,signId, userId, mbcAnimalShelterDTO, now) + count > 1;
        }
        //查询最大的ShelterId值
         maxShelterId = maxShelterId + 1;
        return animalShelterMapper.insertAnimalShelter(maxShelterId,signId, userId, mbcAnimalShelterDTO, now) + count > 1;
    }

    /**
     * 全查和条件查询庇护所
     *
     * @param getMbcAnimalShelterDTO 获取 MBC 动物收容所 DTO
     * @return {@link ArrayList}<{@link GetMbcAnimalShelterVO}>
     */
    @Override
    public ArrayList<GetMbcAnimalShelterVO> getByAnimalShelter(GetMbcAnimalShelterDTO getMbcAnimalShelterDTO) {
        if (getMbcAnimalShelterDTO.getName() != null && getMbcAnimalShelterDTO.getShelterId() != null){
            return new ArrayList<>();
        }
        Integer page = getMbcAnimalShelterDTO.getPage();
        Integer limit = getMbcAnimalShelterDTO.getSize();
        Integer offest = (page - 1) * limit;
        ArrayList<GetMbcAnimalShelterVO> list = animalShelterMapper.getByAnimalShelter(offest,limit,getMbcAnimalShelterDTO);
        return list;
    }

    /**
     * 更新动物收容所
     *
     * @param mbcAnimalShelterDTO MBC 动物收容所 DTO
     * @param userId              用户 ID
     * @return boolean
     */
    @Override
    public boolean updateAnimalShelter(MbcAnimalShelterDTO mbcAnimalShelterDTO,Integer userId) {
        return animalShelterMapper.updateAnimalShelter(mbcAnimalShelterDTO, userId) > 0;
    }

    /**
     * 删除动物收容所
     *
     * @param userId 用户 ID
     * @return boolean
     */
    @Override
    public boolean deleteAnimalShelter(Integer userId) {
        return animalShelterMapper.deleteAnimalShelter(userId) > 0;
    }

    /**
     * 新增动物庇护所创建人的实名认证
     *
     * @param userId                       用户 ID
     * @param mbcRealNameAuthenticationDTO MBC实名认证DTO
     * @return boolean
     */
    @Override
    public boolean insertRealNameAuthentication(Integer userId, MbcRealNameAuthenticationDTO mbcRealNameAuthenticationDTO) {
        LocalDateTime now = LocalDateTime.now();
        return realNameAuthenticationMapper.insertRealNameAuthentication(userId,mbcRealNameAuthenticationDTO,now) > 0;
    }

    /**
     * 根据id查询庇护所详细信息
     *
     * @param shelterId 避难所 ID
     * @return {@link GetMbcAnimalShelterVO}
     */
    @Override
    public GetMbcAnimalShelterVO getByIdAnimalShelter(Integer shelterId) {
        return animalShelterMapper.getByIdAnimalShelter(shelterId);
    }
}
